﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace EditorFrameWork
{
    public static class TypeEx
    {
        public static IEnumerable<Type> GetSubTypesInAssemblies(this Type self)
        {
            return AppDomain.CurrentDomain.GetAssemblies()
                .Where(assembly => assembly.FullName.StartsWith("Assembly")
                )
                .SelectMany((assembly => assembly.GetTypes()))
                .Where((type => type.IsSubclassOf(self)));
        }

        /**
         * 通过类的特性获取子类
         */
        public static IEnumerable<Type> GetSubTypesWithClassAttributeInAssemblies<TClassAttribute>(this Type self)
            where TClassAttribute : Attribute
        {
            return GetSubTypesInAssemblies(self)
                .Where(type => type.GetCustomAttribute<TClassAttribute>() != null);
        }
    }
}